package sample.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import sample.Main;
import sample.entity.Colmuns;
import sample.entity.SelectTedTable;
import sample.utils.MysqlUtil;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class ObjectTable implements Initializable {
    @FXML
    private TableView<Colmuns> colmunsTable;
    @FXML
    private TableColumn<Colmuns, String> field;
    @FXML
    private TableColumn<Colmuns, String> length;

    @FXML
    private TableColumn<Colmuns, String> type;
    @FXML
    private TableColumn<Colmuns, String> is_null;
    @FXML
    private TableColumn<Colmuns, String> key;
    @FXML
    private TableColumn<Colmuns, String> sdefault;
    @FXML
    private TableColumn<Colmuns, String> extra;
    @FXML
    private TableColumn<Colmuns, String> comment;
    @FXML
    private TableColumn<Colmuns, ImageView> image;

    private ObservableList<Colmuns> masterData = FXCollections.observableArrayList();

    public ObjectTable() {
        //  field.setEditable(true);
        String tableName = SelectTedTable.selectTedTable.getTableName();
        System.out.println(SelectTedTable.getSelectTedTable());
        double parentWidth = SelectTedTable.selectTedTable.getParentWidth();
        //  colmunsTable.setPrefWidth(parentWidth);
        System.out.println("Resize:" + colmunsTable);
        Connection connectionOne = MysqlUtil.getConnection();
        try {
            PreparedStatement preparedStatement = connectionOne.prepareStatement("SHOW FULL  COLUMNS FROM " + tableName);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String field = resultSet.getString("Field");
                String type = resultSet.getString("Type");
                String aNull = resultSet.getString("Null");
                String key = resultSet.getString("Key");
                String aDefault = resultSet.getString("Default");
                String extra = resultSet.getString("Extra");
                String comment = resultSet.getString("Comment");
                masterData.add(new Colmuns(new ImageView(new Image(Main.class.getResourceAsStream("column.png"))), field, type, aNull, key, aDefault, extra, comment));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

/*        masterData.add(new Colmuns("Hans", "Muster",1,"HHH","aa","aa"));
        masterData.add(new Colmuns("Hans", "Muster",1,"HHH","aa","aa"));
        masterData.add(new Colmuns("Hans", "Muster",1,"HHH","aa","aa"));*/
    }

    @FXML
    private void initialize() {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        field.setEditable(true);


        field.setCellFactory(TextFieldTableCell.forTableColumn());

        colmunsTable.setEditable(true);
        colmunsTable.getSelectionModel().setCellSelectionEnabled(true);
        image.setCellValueFactory(new PropertyValueFactory<Colmuns, ImageView>("image"));

        field.setCellValueFactory(cellData -> cellData.getValue().fieldProperty());

        type.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
        length.setCellValueFactory(cellData -> cellData.getValue().lengthProperty());
        String[] a = {"Int", "Varchar"};

        type.setCellFactory(ComboBoxTableCell.forTableColumn(a));

        is_null.setCellValueFactory(cellData -> cellData.getValue().is_nullProperty());
        key.setCellValueFactory(cellData -> cellData.getValue().keyProperty());
        sdefault.setCellValueFactory(cellData -> cellData.getValue().sdefaultProperty());
        extra.setCellValueFactory(cellData -> cellData.getValue().extraProperty());
        comment.setCellValueFactory(cellData -> cellData.getValue().commentProperty());
        //    masterData.
        colmunsTable.setItems(masterData);
    }
}
